using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Pricom
{
  public partial class ChatPage : UserControl
  {
    public ChatPage(TabPage _tabPage, IConnectionProvider _connection)
    {
      connection = _connection;
      ownerTabPage = _tabPage;

      InitializeComponent();
    }

    #region Properties

    public TabPage TabPage
    {
      get { return ownerTabPage; }
    }

    public Controls.HistoryTextBox InputBox
    {
      get { return input; }
    }

    public string Title
    {
      get { return ownerTabPage.Text; }
      set { ownerTabPage.Text = value; }
    }

    #endregion Properties

    #region Public Methods

    public void WriteOutput(string message)
    {
      string newLine = string.Empty;
      if (output.Text.Length != 0)
        newLine = Environment.NewLine;
      string line = String.Concat(newLine, String.Format("[{0:D2}:{1:D2}]", DateTime.Now.TimeOfDay.Hours, DateTime.Now.TimeOfDay.Minutes), message);
      line = line.TrimEnd();
      output.AppendText(line);
      ScrollDown();
    }

    public void MarkTab(bool read)
    {
      if (ownerTabPage.InvokeRequired)
        ownerTabPage.BeginInvoke(new System.Windows.Forms.MethodInvoker(delegate() { MarkTab(read); }));
      else
      {
        if (ownerTabPage.Text.StartsWith("*") && read || !ownerTabPage.Text.StartsWith("*") && !read)
          return;
        ownerTabPage.Text = (read ? "* " + ownerTabPage.Text : ownerTabPage.Text.Replace("* ",""));
      }
    }

    public void ClearOutput()
    {
      output.Clear();
    }

    public void ClearNicknames()
    {
      nickNames.Clear();
    }

    public void AddNickname(string nickName)
    {
      nickNames.Add(nickName);
    }

    public void HideNickPanel()
    {
      splitContainer1.Panel2Collapsed = true;
    }

    public void FocusInput()
    {
      input.Focus();
    }

    public void ScrollDown()
    {
      //WinAPI.ScrollDown(output.Handle);
      if (output.InvokeRequired)
        output.BeginInvoke(new System.Windows.Forms.MethodInvoker(delegate() { ScrollDown(); }));
      else
      {
        output.SelectionStart = 0;
        output.ScrollToCaret();
        output.SelectionStart = output.Text.Length;
        output.ScrollToCaret();
      }
    }

    #endregion Public Methods

    #region Events

    private void output_LinkClicked(object sender, LinkClickedEventArgs e)
    {
      System.Diagnostics.Process.Start(e.LinkText);
    }

    private void output_MouseUp(object sender, MouseEventArgs e)
    {
      if (output.SelectedText.Length > 0)
        Clipboard.SetText(output.SelectedText);
      FocusInput();
    }

    private void nickNames_MouseDoubleClick(object sender, MouseEventArgs e)
    {
      int i = nickNames.IndexFromPoint(e.X, e.Y);
      if (i == ListBox.NoMatches)
        return;

      string nickName = nickNames.Items[i].ToString();
      if (nickName.StartsWith("@"))
        nickName = nickName.Remove(0, 1);
      string url = connection.ToString() + "/" + nickName;
      if (StartQueryMessage != null)
        StartQueryMessage(nickNames.Items[i].ToString(), url);
    }

    private void input_LineEntered(object sender, string line)
    {
      if (!connection.IsConnected() || connection.GetNickname().Length == 0)
        return;
      string url = ownerTabPage.Tag.ToString();
      string channel = url.Substring(url.LastIndexOf('/') + 1);
      if (line.StartsWith("/"))
        connection.Command(line.Remove(0, 1));
      else
      {
        connection.Send(channel, line);
        WriteOutput(String.Concat(" <", connection.GetNickname(), "> ", line));
      }
    }

    #endregion Events

    private IConnectionProvider connection;
  }
}
